package com.linjacy.httpproject.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.linjacy.httpproject.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class TestServiceImpl implements TestService {

    @Autowired
    private RestTemplate restTemplate;
    @Value("${city.classify.url}")
    String classifyUrl;

    @Override
    public void testRest() {
        HttpHeaders headers = new HttpHeaders();
        headers.add("token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2NTM2NTg3NDEsInVzZXJJZCI6ImIxMzE4MWI1OTUyMjQ1ZjU5NTM2NzFhNTliN2RlMWUwIn0.5ecEkzCBNCvxJrYYIp5la8LYKpBBiMnoBx6HEl2vXSo");
        headers.add("sysTenantId", "drcpm");
        headers.add("sysMenuId", "e61d6f315c22415d96fabf383965e806");
        Map<String,String> param = new HashMap<>();
        //param.put("tableId","238e8f4df5bf42c48b3a95d2a9a0a36a");
        //param.put("classifyType","SZDXFHY");
        //param.put("queryDate","202205");
        //String url = "http://192.168.100.156:24000/drcpm-api/drcpm/itemReportBranch/cityClassifySta";
        String tableId = "238e8f4df5bf42c48b3a95d2a9a0a36a";
        String classifyType = "SZDXFHY";
        String queryDate = "202205";
        String url = classifyUrl;
        ResponseEntity<JSONObject> exchange = restTemplate.exchange(
                MessageFormat.format(url, tableId, classifyType, queryDate),
                HttpMethod.GET,
                new HttpEntity<String>(headers),
                JSONObject.class, param);
        JSONObject body = exchange.getBody();
        Map map = Optional.ofNullable(JSONObject.parseObject(String.valueOf(body), Map.class)).orElseGet(HashMap::new);
        List<Map<String,String>> dataList = JSONObject.parseObject(JSONObject.toJSONString(map.get("data")), List.class);
        System.out.println(dataList);
    }
}
